home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / MiniExamples / AppKit / ConvertXYtoChar / TurboText.m < prev   
Text File  |  1995-06-12  |  3KB  |  93 lines

  1. /* TurboText.m
  2.  * Purpose:  A subclass of the Text object.  This is necessary in order
  3.  *     to over-ride mouseDown.  The -convertPoint: method posts a fake
  4.  *    mouse up event at the desired x,y coordinate location.  It then sets
  5.  *    an internal flag and calls mouseDown with a fake mouse down event.
  6.  *    The end result is that the Text object will believe that the user has
  7.  *    clicked the mouse on the desired location and will cause a selection
  8.  *    at the appropriate point.
  9.  *
  10.  * You may freely copy, distribute, and reuse the code in this example.
  11.  * NeXT disclaims any warranty of any kind, expressed or  implied, as to its
  12.  * fitness for any particular use.
  13.  *
  14.  * Written by: Sharon Zakhour
  15.  * Created: 22/July/91
  16.  *
  17.  */
  18.  
  19. #import <dpsclient/event.h>
  20. #import "TurboText.h"
  21.  
  22. @implementation TurboText
  23.  
  24. - initFrame: (const NXRect *)frameRect
  25. {
  26.     [super initFrame:frameRect];
  27.     convert = NO;
  28.     charPos = -1;
  29.     return self;
  30. }
  31.  
  32. - mouseDown:(NXEvent *)theEvent
  33. {
  34.     NXSelPt    origStart, origEnd;
  35.     
  36.     if (convert)
  37.     {
  38.         /* Squirrel away the original selection */
  39.         [self getSel: &origStart  :&origEnd];
  40.         
  41.         /* This is to hide the modified selection from the user */
  42.         /* We want to avoid any flickering the user may see */
  43.         [window disableFlushWindow];
  44.     }
  45.     
  46.     /* Let the mouseDown in the text object do it's stuff... */
  47.     [super mouseDown:theEvent];
  48.     
  49.     if (convert)
  50.     {
  51.         NXSelPt    newStart, newEnd;
  52.         
  53.         /* Get the new character position and snarf it away */
  54.         [self getSel: &newStart  : &newEnd];
  55.         charPos = newStart.cp;
  56.         
  57.         /* Restore the original selection */
  58.         [self setSel: origStart.cp  :origEnd.cp];
  59.         [window reenableFlushWindow];
  60.         
  61.         convert = NO;
  62.     }
  63.     return self;
  64. }
  65.  
  66. - (int)convertPoint: (NXPoint *)myPoint
  67. {
  68.     NXEvent postEvent = (NXEvent)*[NXApp currentEvent]; 
  69.     
  70.     /* Post the mouse up event to the queue */
  71.     convert = YES;    
  72.     [self convertPoint: myPoint toView:nil];
  73.     postEvent.location.x = myPoint->x;
  74.     postEvent.location.y = myPoint->y;
  75.     postEvent.type = NX_MOUSEUP;
  76.     DPSPostEvent(&postEvent, YES);
  77.     
  78.     /* Call the Text object with the mouse down event...  */
  79.     /* It will process the mouse down event followed by the */
  80.     /* mouse up event which it will pluck off the queue.  */
  81.     /* These fake events will cause the mouseDown code */
  82.     /* to make a selection at the desired location.  The character */
  83.     /* position can then be extracted from the start point of the */
  84.     /* selection.   */
  85.     postEvent.type = NX_MOUSEDOWN;
  86.     postEvent.data.mouse.click = 1;
  87.     [self mouseDown:&postEvent];
  88.     return charPos;
  89. }
  90.  
  91.  
  92. @end
  93.